Skip to content

Evaluate fixed-operand effects before hoisted emplacement construction - #675

Merged
nunoplopes merged 1 commit into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-rhkhpqe3kj
Nov 21, 2026
Merged

nunoplopes merged 1 commit into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-rhkhpqe3kj

Conversation

@perfloop-agent

Copy link
Copy Markdown
Contributor

Since a495c8d ("don't require rules to worry about run-time borrow errors"), GetMappedAsString may construct an emplaced element (init) before the rule body runs. The receiver and the emplace_hint hint (the operands before the forwarded pack) are still evaluated later, inside the body. In C++ they are evaluated before the element is constructed. So the translation can copy stale data in both models, and in the refcount model an aliased receiver can panic with "RefCell already mutably borrowed".

Example (tests/unit/emplace_receiver_order.cpp): select_rows(&rows, &calls)->emplace_back(rows[0].data(), rows[0].data() + 3), where select_rows sets rows[0][2] = 9. Native output inserts 9. The translation inserts 3 in both models.

When a rule always constructs init and is called on a receiver, GetMappedAsString now marks the receiver and any other non-pack operands that have side effects for binding, before it checks whether init must be constructed first. If construction is hoisted, those operands are evaluated once, in C++ order, before construction and before the container is borrowed mutably. Evaluation uses the existing binding code. If construction is not hoisted, the original analysis and output are unchanged. Binding only after the hoisting decision fixes the wrong values but not the aliased-receiver panic, because there the binding is what allows construction to be hoisted.

HasSideEffects treats library calls as effectful, so hints such as m.end() or m.find(k) are now also bound first. This keeps their meaning. It adds one iterator binding (and a no-op clone) to the refcount output, which is why tests/unit/out/refcount/map.rs changes, with renumbered temporaries.

Tests:

  • tests/unit/emplace_receiver_order.cpp: wrong value, plus a control with a separately bound receiver.
  • tests/unit/emplace_receiver_alias.cpp: refcount panic through a pointer alias of the receiver.
  • tests/unit/emplace_hint_receiver_order.cpp: hint side effects with a selected receiver and with a plain map.

All three pass after this change, in both models. On Linux with clang 23, Release lit reports 649 passed and 33 expected failures, with the two socket cases below excluded; focused Debug lit reports 16 passed and 2 expected failures. The changed C++ files pass clang-format.

tests/unit/socket_ip_addrlen.c and tests/unit/socket_recvfrom_name.c encounter native socket assertion failures on both revisions and remain for target CI.

Not changed: side effects inside the forwarded arguments, e.g. p.emplace_back(v, mod(&v)), are still evaluated in a different order from C++. try_emplace, which constructs only if the key is missing, is not affected. Hint performance (#611) is separate.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
@nunoplopes
nunoplopes merged commit c902b60 into Cpp2Rust:master Nov 21, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants